package com.epmxx.controller;

import com.epmxx.common.Result;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Description: 用户控制类
 */
@Controller
@RequestMapping("user")
public class UserController {
    /**
     * 用户登录的请求 处理方法
     *
     * @return
     */
    @PostMapping("doLogin.do")
    @ResponseBody
    public Result login(String username, String password) {
        // 使用用户名和密码获取认证 token
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);
        // 获取认证主体
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(usernamePasswordToken);
        } catch (AuthenticationException e) {
            e.printStackTrace();
            System.err.println(" ============ 登录发生了异常 ============= ");
            return new Result(500, "登录异常");
        }
        return new Result();
    }

    /**
     * 登录成功跳转到酒店房间列表页面
     *
     * @return
     */
    @GetMapping("toList.do")
    public String toList() {
        return "roomList";
    }

}
